<template>
	<view class="login-container">
		<u--form :rules="rules" :model="user" ref="uForm">
			<h2  class="loginTitle">注册</h2>
			<u-form-item borderBottom prop="userName" >
				<u--input v-model="user.userName" border="none" prefixIcon="account" placeholder="用户名" clearable></u--input>
			</u-form-item>
			<u-form-item  borderBottom prop="nickName">
				<u--input v-model="user.nickName" border="none" prefixIcon="man-add" placeholder="昵称" clearable></u--input>
			</u-form-item>
			<u-form-item  borderBottom prop="phonenumber" >
				<u--input v-model="user.phonenumber" border="none" type="text" prefixIcon="phone" placeholder="手机号" clearable></u--input>
			</u-form-item>
			<u-form-item  borderBottom prop="password">
				<u--input v-model="user.password" border="none" placeholder="密码" prefixIcon="lock"></u--input>
			</u-form-item>
			<u-form-item borderBottom prop="RepeatPassword" >
				<u--input v-model="user.RepeatPassword" border="none" placeholder="确认密码" prefixIcon="lock-fill"></u--input>
			</u-form-item>
			<view class="u-page">
				<u--text type="primary" text="已有账号去登录" align="right" style="margin: 10px -10px;" @click="toLogin"></u--text>
				<u-row>
					<u-col>
						<u-button  @click="submit">注册</u-button>
					</u-col>
					
				</u-row>
			</view>
		</u--form>
		
	</view>
</template>
<script>
export default {
	data() {
		return {
			title:'标题',
			user:{
				userName:'',
				nickName:"",
				phonenumber:'',
				password:"",
				RepeatPassword:'',
				sex:'男'
			},
			rules: {
				'userName': {
					type: 'string',
					required: true,
					message: '请填写用户名',
					trigger: ['blur', 'change']
				},
				'phonenumber': {
					type: 'string',
					min:6,
					max: 16,
					required: true,
					message: '请输入密码',
					trigger: ['blur', 'change']
				},
				'phonenumber': [
					{
								required: true, 
								message: '请输入手机号',
								trigger: ['change','blur'],
							},
							{
								pattern:/^1[3-9]\d{9}$/,
								message: '请填写正确的手机号',
								trigger: ['change'],
							}
				],
				'RepeatPassword':[
					{
						type: 'string',
						min:6,
						max: 16,
						required: true,
						message: '请输入确认密码',
						trigger: ['blur', 'change']
					},
					{
					validator:(rule,value,callblack)=>{
						if(value !== this.user.password){
							console.log("1111");
							callback(new Error('两个密码必须一致'))
						}else{
							callback()
						}
					},
					trigger: ['blur', 'change'],
				
				},
				]
			},
			show: false,
			
		};
		
	},
	methods:{
		submit(){
			this.$refs.uForm.validate().then(res=>{
				this.$request({
					url:'/prod-api/api/register',
					method:'POST',
					data:this.user
				}).then(res=>{
					console.log(res);
					if(res.statusCode=='200'){
						if(res.data.code=='200'){
							uni.$u.toast('注册成功')
						}else{
							uni.$u.toast(res.data.msg)
							
						}
					}else{
						console.log("服务器错误,联系管理员处理",);
					}
				}).catch(err=>{
					
				})
			})
		},
		toLogin(){
			uni.navigateTo({
				url:'/pages/login/login'
			})
		},
	}
};
</script>

<style lang="scss">
	.login-container{
		.loginTitle{
			text-align: center;
		}
		.u-form{
			padding:15rpx 20rpx;
		}
	}
	
</style>